﻿/**************************************************************************
创建日期:	2019/7/22 10:54:38     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述： 增删查改的抽象实现
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.DbCore.Cores;
using ZhCun.DbCore.Entitys;
using ZhCun.WebCommon.ViewModels;

namespace ZhCun.WebCommon.AL
{
    /// <summary>
    /// 增删查改的al基类，用于List 实体 与 Save 实体相同的实现
    /// </summary>
    public abstract class ALCRUDBase<TListArg, TDBModel> : ALCRUDBase<TListArg, TDBModel, TDBModel>
        where TListArg : ArgGetList, new()
        where TDBModel : EntityBase, new()
    { }

    /// <summary>
    /// 增删查改的al基类,List类型与save类型分开
    /// </summary>
    /// <typeparam name="TListArg">获取列表的参数类型</typeparam>
    /// <typeparam name="TSaveModel">实体对象类型</typeparam>
    /// <typeparam name="TListModel">查询List实体类型</typeparam>
    public abstract class ALCRUDBase<TListArg, TSaveModel, TListModel> : ALCRUD<TListArg, TSaveModel, TListModel> //ALReadPagerBase<TListArg, TListModel>
        where TListArg : ArgGetList, new()
        where TSaveModel : EntityBase, new()
        where TListModel : EntityBase, new()
    {
        protected abstract bool CheckSaveArg(TSaveModel model, bool isAdd, out string msg);

        protected abstract ExecResult GetDelResult(string id);

        public override bool Add(TSaveModel model, out string msg)
        {
            if (!CheckSaveArg(model, true, out msg))
            {
                return false;
            }
            var r = DB.Insert(model);
            if (r.RowCount == 1)
            {
                msg = "保存成功";
                return true;
            }
            else
            {
                msg = "保存失败";
                return false;
            }
        }

        public override bool Update(TSaveModel model, out string msg)
        {
            if (!CheckSaveArg(model, false, out msg))
            {
                return false;
            }
            var r = DB.Update(model);
            if (r.RowCount == 1)
            {
                msg = "保存成功";
                return true;
            }
            else
            {
                msg = "保存失败";
                return false;
            }
        }

        public override bool Del(string id, out string msg)
        {
            var r = GetDelResult(id);
            if (r.RowCount == 1)
            {
                msg = "删除成功";
                return true;
            }
            else
            {
                msg = "删除失败";
                return false;
            }
        }
    }

    public abstract class ALCRUD<TListArg, TSaveModel> : ALCRUD<TListArg, TSaveModel, TSaveModel>
        where TListArg : ArgGetList, new()
        where TSaveModel : EntityBase, new()
    { }

    public abstract class ALCRUD<TListArg, TSaveModel, TListModel> : ALReadPagerBase<TListArg, TListModel>
        where TListArg : ArgGetList, new()
        where TListModel : EntityBase, new()
    {
        public abstract bool Add(TSaveModel model, out string msg);

        public abstract bool Update(TSaveModel model, out string msg);

        public abstract bool Del(string id, out string msg);
    }
}